package org.ycx.generator.step;

import org.apache.velocity.VelocityContext;
import org.ycx.common.annotation.FormDesc;
import org.ycx.common.util.ClassUtil;
import org.ycx.common.util.StringUtil;
import org.ycx.common.util.VelocityUtil;
import org.ycx.generator.GenNode;
import org.ycx.generator.IGeneratorStep;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.ycx.common.util.StringUtil.humpToLine2;

/**
 * Created by 杨 on 2017-07-18.
 */
public class ControllerStep implements IGeneratorStep {


    public Set<String> ignoreFields = new HashSet<String>();

    private void init() {
        ignoreFields.add("getCreateTs");
        ignoreFields.add("getUpdateTs");
        ignoreFields.add("getCreateUid");
        ignoreFields.add("getUpdateUid");
        ignoreFields.add("getDelState");
    }

    @Override
    public VelocityContext initContext(VelocityContext context, String className) throws Exception {
        Class clazz = Class.forName(className);
        context.put("model", clazz.getSimpleName());
        context.put("conName", clazz.getSimpleName());
        context.put("pk", ((FormDesc) clazz.getAnnotation(FormDesc.class)).pk());
        context.put("URI", humpToLine2(clazz.getSimpleName()).replaceAll("_", "/").replaceFirst("/", ""));
        context.put("nodes", modelsGen(clazz));
        return context;
    }

    private List<GenNode> modelsGen(Class clazz) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        List<GenNode> nodes = new ArrayList<GenNode>();
        //获取自动model的字段
        for (int i = 0; i < fields.length; ++i) {
            if (!this.ignoreFields.contains("get" + StringUtil.toUpperCaseFirstOne(fields[i].getName())) && !Modifier.isStatic(fields[i].getModifiers())) {
                FormDesc desc = fields[i].getAnnotation(FormDesc.class);
                if(desc == null){
                    continue;
                }
                GenNode node = new GenNode(desc.name(), "get" + StringUtil.toUpperCaseFirstOne(fields[i].getName()), desc.maxLength());
                node.setType(desc.type());
                if (desc.type() == 4) {
                    //选择框
                    Method method = desc.choice().getMethod("values");
                    node.setChoices((Object[]) method.invoke(desc.choice()));
                }
                node.setPrecision(desc.precision());
                nodes.add(node);
            }
        }
        return nodes;
    }

    @Override
    public String getOutFileName(String loadName) {
        try {
            Class clazz = Class.forName(loadName);
            if (((FormDesc) clazz.getAnnotation(FormDesc.class)).isMidTable()) {
                return null;
            }
            String model = clazz.getSimpleName();
            return model+"Controller.java";
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
    @Override
    public boolean overideFile(String loadName) {
        File file = new File(loadName);
        return !file.exists();
    }

    @Override
    public String getProjectPath(String module) {
        return System.getProperty("user.dir") + File.separator + module;
    }

    @Override
    public String getVmFile() {
        return "/templates/vm/Controller.vm";
    }
}
